package com.niit.controller;

import com.niit.apis.NotificationFeignApi;
import com.niit.entity.Notification;
import com.niit.entity.User;
import com.niit.utils.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notification")
public class NotificationController {
	private final NotificationFeignApi notificationFeignApi;

	public NotificationController(NotificationFeignApi notificationFeignApi) {
		this.notificationFeignApi = notificationFeignApi;
	}

	@PostMapping("/getAll")
	public Result<?> getNotifications(
			@RequestParam(required = false) String title,
			@RequestParam(required = false) Byte level,
			@RequestParam(required = false) Byte type,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size,
			HttpSession session) {
		try {
			Integer loginType = (Integer) session.getAttribute("loginType");
			Page<Notification> notifications = notificationFeignApi.findNotifications(
					title, level, type, page, size, loginType);
			return Result.successPage(notifications.getContent(), notifications.getTotalElements()).code(200);
		} catch (Exception e) {
			return Result.error("获取通知列表失败");
		}
	}


	@PostMapping
	public Result<?> createNotification(@RequestBody Notification notification, HttpSession session) {
		Integer loginType = (Integer) session.getAttribute("loginType");
		if (loginType == 3) {
			notificationFeignApi.createNotification(notification);
			return Result.success("添加成功");
		}
		return Result.error("用户权限不足");
	}

	@PutMapping("/{id}")
	public Result<?> updateNotification(
			@PathVariable Integer id,
			@RequestBody Notification notification,
			HttpSession session) {
		Integer loginType = (Integer) session.getAttribute("loginType");
		if (loginType == 3) {
			notificationFeignApi.updateNotification(id, notification);
			return Result.success("更新成功");
		}
		return Result.error("用户权限不足");
	}

	@DeleteMapping("/{id}")
	public Result<?> deleteNotification(@PathVariable Integer id, HttpSession session) {
		Integer loginType = (Integer) session.getAttribute("loginType");
		if (loginType == 3) {
			notificationFeignApi.deleteNotification(id);
			return Result.success("删除成功");
		}
		return Result.error("用户权限不足");
	}

	@DeleteMapping("/batch")
	public Result<?> deleteNotifications(@RequestBody List<Integer> ids, HttpSession session) {
		Integer loginType = (Integer) session.getAttribute("loginType");
		if (loginType == 3) {
			notificationFeignApi.deleteNotifications(ids);
			return Result.success("批量删除通知成功");
		}
        return Result.error("用户权限不足");
    }

	@PostMapping("/read/{notificationId}")
	public Result<?> markAsRead(@PathVariable Integer notificationId, HttpSession session) {
		try {
			Object account = session.getAttribute("account");
			if (account instanceof User user) {
				notificationFeignApi.markAsRead(user.getId(), notificationId);
				return Result.success();
			}
			return Result.error(401, "未登录");
		} catch (Exception e) {
			return Result.error("标记已读失败");
		}
	}
}
